package com.bionic.banking.commands;

import com.bionic.banking.auth.manager.Config;
import com.bionic.banking.core.entity.Customer;
import com.bionic.banking.core.entity.User;
import com.bionic.banking.dao.connection.LoggerLoader;
import com.bionic.banking.logic.controller.CustomerController;
import com.bionic.banking.logic.controller.UserController;
import com.bionic.banking.logic.exception.InvalidParameterException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CommandViewMyProfile implements ICommand {

    private static final org.apache.log4j.Logger logger =
            LoggerLoader.getLogger(CommandViewMyProfile.class);

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException {
        String page = null;

        try {
            HttpSession session = request.getSession(false);
            if (session != null) {
                String login = (String) session.getAttribute("user");
                UserController userBean = new UserController();
                User currentUser = userBean.getByLogin(login);
                Integer userId = currentUser.getUserId();

                CustomerController customerBean = new CustomerController();
                Customer customer = customerBean.getCustomerByUserId(userId);
                Integer customerId = customer.getCustomerId();

                String contentPage = Config.getInstance().getProperty(Config.EDIT_CUSTOMER)
                        + "?id=" + customerId;
                request.setAttribute("content", contentPage);
                page = Config.getInstance().getProperty(Config.MAIN);
            }
            // to show in main content
        } catch (InstantiationException | IllegalAccessException | SQLException 
                | InvalidParameterException ex) {
            logger.error(ex);
            request.setAttribute("error", ex.getMessage());
            page = Config.getInstance().getProperty(Config.ERROR);
        }
        return page;
    }
}
